package pers.cz.thread;

import java.util.Map;
import java.util.Set;

/**
 * @program: PostGirl-panent
 * @description:
 * @author: Cheng Zhi
 * @create: 2022-05-03 22:45
 **/
public class Test {

    public static void main(String[] args) {
        long startTime = System.nanoTime();
        Master master = new Master(new TestWorker(), 15);
        for (int i= 0; i<10000; i++) {
            master.submit(i, i);
        }

        master.execute();
        master.stop();
        int re = 0;
        Map<String,Object> resultMap=master.getResultMap();
        //不需要等待所有的Worker都执行完即可进行求和操作 || !master.isComplete()
        while(resultMap.size()>0 ){
            Set<String> keys=resultMap.keySet(); //开始计算最终结果
            String key=null;
            for(String k:keys){
                key=k;
                break;
            }
            Integer i=null;
            if(key!=null)
                i=(Integer)resultMap.get(key);
            if(i!=null)
                re+=i; //最终结果
            if(key!=null)
                resultMap.remove(key); //移除已经被计算

        }
        long endTime = System.nanoTime();
        System.out.println("结果为：" + re + "  耗时：" + (endTime-startTime));

    }
}
